package com.superdry.adapter;

import java.util.List;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSONObject;
import com.drysurper.R;
import com.superdry.activity.PayOrderActivity;
import com.superdry.json.OrderListItemBean;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.ToastHelper;

import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class OrderListAdapter extends BaseAdapter {

	private Context context;
	private List<OrderListItemBean> items;
	private AbHttpUtil myHttpUtils;

	public OrderListAdapter(Context context, List<OrderListItemBean> items) {
		this.context = context;
		this.items = items;
		myHttpUtils = AbHttpUtil.getInstance(context);
		myHttpUtils.setTimeout(1000);
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public OrderListItemBean getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder;
		final OrderListItemBean item = getItem(position);
		if (convertView == null) {
			viewHolder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.fragment_order_item, parent, false);

			viewHolder.order_id = (TextView) convertView.findViewById(R.id.order_id);
			viewHolder.order_cost = (TextView) convertView.findViewById(R.id.order_cost);
			viewHolder.order_time = (TextView) convertView.findViewById(R.id.order_time);
			viewHolder.order_status_01 = (TextView) convertView.findViewById(R.id.order_status_01);
			viewHolder.order_status_02 = (TextView) convertView.findViewById(R.id.order_status_02);
			viewHolder.order_progress = (ImageView) convertView.findViewById(R.id.order_progress);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.order_id.setText(item.getOrderNO());
		viewHolder.order_cost.setText(item.getSurplusMoney());
		viewHolder.order_time.setText(item.getOrderTime());
		if (item.getTakeStatus() == 0) {
			// 未付款
		} else {
			// 已付款
			viewHolder.order_status_01.setVisibility(View.GONE);
			viewHolder.order_status_02.setText("已付款 ");
		}

		setProgressDrawable(item, viewHolder.order_progress);
		
		viewHolder.order_status_01.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				// 取消订单
				AbRequestParams params = new AbRequestParams();
				params.put("orderID", item.getOrderID());
				myHttpUtils.post(GlobalConst.GET_DEL_ORDER, params, new AbStringHttpResponseListener() {

					@Override
					public void onStart() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onFinish() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onFailure(int statusCode, String content, Throwable error) {
						// TODO Auto-generated method stub
						ToastHelper.show(context, "取消失败");

					}

					@Override
					public void onSuccess(int statusCode, String content) {
						JSONObject obj = JSONObject.parseObject(content);
						int result = obj.getIntValue("i");
						if (result == 1) {
							ToastHelper.show(context, "取消成功");
							items.remove(item);
							notifyDataSetChanged();
						} else {
							ToastHelper.show(context, "取消失败");
						}
					}
				});
			}
		});
		viewHolder.order_status_02.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				
				String str = ((TextView)view).getText().toString();
				if("未付款".equals(str.trim())){
					Intent intent = new Intent(context,PayOrderActivity.class);
					intent.putExtra("order", item);
					context.startActivity(intent);
				}
			}
		});
		
		
		return convertView;
	}

	/**
	 * 设置进度图片
	 */
	private void setProgressDrawable(OrderListItemBean item, ImageView progress) {
		
		if (item.getTakeStatus() == 0) {
			// 未付款
			progress.setImageResource(R.drawable.order_progress_placed);
		} else {
			// 已付款
			progress.setImageResource(R.drawable.order_progress_payed);
			if(item.getOrderStatus() == 3){
				//清洗中
				progress.setImageResource(R.drawable.order_progress_drying);
			} else if(item.getOrderStatus() == 5){
				//已完成
				progress.setImageResource(R.drawable.order_progress_finished);
			}
			
			
			
		}

	}

	class ViewHolder {
		TextView order_id;
		TextView order_cost;
		TextView order_time;
		TextView order_status_01;
		TextView order_status_02;
		ImageView order_progress;
	}
}
